//
//  CollectDatabase.m
//  A段项目音乐播放
//
//  Created by dllo on 15/5/30.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "CollectDatabase.h"
#import "SingerModel.h"
#import "CollectDB.h"
@implementation CollectDatabase


+ (void)insertWithModel:(SingerModel *)model
{
    NSString *str = [NSString stringWithFormat:@"insert into Liet(name,singerName,url)values('%@','%@','%@')",model.name,model.singerName,model.url];
    
    sqlite3 *db = [CollectDB openDB];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}



+ (void)deleteWithModel:(SingerModel *)model
{
    NSString *str = [NSString stringWithFormat:@"delete from Liet where name = '%@'",model.name];
    sqlite3 *db = [CollectDB openDB];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}



+ (void)updateWithModel:(SingerModel *)model
{
    
}


+ (NSArray *)selectAll
{
    NSString *str = @"select name,singerName,url from Liet";
    
    sqlite3 *db = [CollectDB openDB];
    
    // 定义执行结果接受者
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *name = sqlite3_column_text(stmt, 0);
            const unsigned char *singerName = sqlite3_column_text(stmt, 1);
            const unsigned char *url = sqlite3_column_text(stmt, 2);
            
            NSString *name1 = [NSString stringWithUTF8String:(const char *)name];
            NSString *singerName1 = [NSString stringWithUTF8String:(const char *)singerName];
            NSString *url1 = [NSString stringWithUTF8String:(const char *)url];
            
            SingerModel *model = [[SingerModel alloc]init];
            model.name = name1;
            model.singerName = singerName1;
            model.url = url1;

            [array addObject:model];
        }
    }
    return array;
    
}







@end
